<?php
	// Achievements coded by Char with love~
	// (Coordinates stolen from FrenchCoderGuy2834)
	
	require("connect.php");
	
	// Functions!
	function db_secure($in) {
		return mysql_real_escape_string($in);
	}
	function imagefillroundedrect($im,$x,$y,$cx,$cy,$rad,$col) { // Stolen from somewhere
		imagefilledrectangle($im,$x,$y+$rad,$cx,$cy-$rad,$col);
		imagefilledrectangle($im,$x+$rad,$y,$cx-$rad,$cy,$col);
		$dia = $rad*2;
		imagefilledellipse($im, $x+$rad, $y+$rad, $rad*2, $dia, $col);
		imagefilledellipse($im, $x+$rad, $cy-$rad, $rad*2, $dia, $col);
		imagefilledellipse($im, $cx-$rad, $cy-$rad, $rad*2, $dia, $col);
		imagefilledellipse($im, $cx-$rad, $y+$rad, $rad*2, $dia, $col);
	}
	function errorc($er) {
		$img = imagecreate(436, 72);
		$cBg = imagecolorallocate($img, 0, 0, 0);
		$cText = imagecolorallocate($img, 255, 255, 255);
		imagestring($img, 2, 4, 4,  $er, $cText);
		imagepng($img);
		imagedestroy($img);
		die();
	}
	
	// Get variables
	$steamID = @empty($_GET['u']) ? null : strtoupper($_GET['u']);
	$amount = @empty($_GET['am']) ? 0 : $_GET['am'];
	$finished = @empty($_GET['af']) ? 0 : $_GET['af'];
	$title = @empty($_GET['at']) ? null : strtoupper($_GET['at']);
	$description = @empty($_GET['ad']) ? null : strtoupper($_GET['ad']);
	$image = @empty($_GET['aimg']) ? null : $_GET['aimg'];
	$achID = @empty($_GET['a']) ? null : $_GET['a'];
	$mode = @empty($_GET['m']) ? 0 : $_GET['m'];
	if($mode != 2)
		if ($achID == null) errorc("Achievement ID unspecified.");
	if ($mode == 1) $steamID = null;
	if ($mode == 2) $steamID = null;
	
	// Queries
	if($mode==2)
	{
		$damount = $amount;
		$percentage = "";
	}
	else
	{
		if ($steamID != null) {
			$q1 = mysql_query('SELECT status,complete FROM achmw_users_ach WHERE steamid = \''.db_secure($steamID).'\' AND achid = \''.db_secure($achID).'\'');
			$d1 = mysql_fetch_assoc($q1);
		}
		$q2 = mysql_query('SELECT name,description,amount,img FROM achmw_achievements WHERE id = '.db_secure($achID));
		$d2 = mysql_fetch_assoc($q2);
		
		// Error handling
		if ($d2['name'] == null) errorc("Invalid Achievement ID");
		$damount = $d2['amount'];
	}
	// Colours
	$img = imagecreatetruecolor(436, 72);
	$bg = imagecolorallocate($img, 79, 79, 79);
	$bgd = imagecolorallocate($img, 52, 52, 52);
	$color = imagecolorallocate($img, 144, 144, 144);
	$progress = imagecolorallocate($img, 200, 185, 150);
	$red = imagecolorallocate($img, 255, 0, 0);
	$progressbg = imagecolorallocate($img, 26, 26, 26);
	$colorcomplete = imagecolorallocate($img, 157, 195, 76);
	$trans = imagecolorallocate($img, 38, 238, 165);
	imagecolortransparent($img, $trans);
	$font  = './font/tf2secondary.ttf';
	$bfont = './font/tf2build.ttf';
    $xProText = 0;
	
	if($mode==0)
	{
		if ($steamID != null) {
			$status = $d1['complete'];
			$fill = ($d1['status'] == null ? 0 : $d1['status']);
			$overview = 0;
		}
		else 
		{
			$fill = 0;
			$overview = 1;
		}
	}
	else 
	{
		if($finished != 0)
			$fill = $damount;
		else
			$fill = 0;
        $overview = 1;
	}
	
	if($mode == 1 || $mode == 2)
	{
		$status = 1;
	}
    
	if ($status) {
		$cTitle = $colorcomplete;
		$cDescr = $progress;
		$cProTxt = $progress;
		$cProgress = $progress;
		$cProgressbg = $progressbg;
		$cBg = $bg;
	}
	else {
		$cTitle = $color;
		$cDescr = $color;
		$cProTxt = $color;
		$cProgress = $progress;
		$cProgressbg = $progressbg;
		$cBg = $bgd;
	}
    
    $tProTxt = ($fill > $damount ? $damount : $fill)."/".$damount;
    
    if ($overview > 0) {
		//$cProTxt = $progress;
		//$cProgress = $progress;
		//$cProgressbg = $progressbg;
        //$cTitle = $progress;
        //$cDescr = $progress;
        //$cBg = $bg;
    }
    if ($mode == 1) {
        $cProgress = $colorcomplete;
        $xProText = -130 - 10;
        $q3 = mysql_query('SELECT * FROM achmw_users');
        $l3 = mysql_num_rows($q3);
        $users = $l3;
        if ($users < 1) $users = 1;
        
        $q4 = mysql_query('SELECT * FROM `achmw_users_ach` WHERE `achid` = \''.db_secure($achID).'\' AND `complete` = \'1\'');
        $l4 = mysql_num_rows($q4);
        $unlocknr = $l4;
        $damount = 100;
        $fill = round($unlocknr / $users *  $damount);
        
        $tProTxt = ($fill > $damount ? $damount : $fill)."%";
		if ($tProTxt > 40)
		$cProTxt = $red;
		else
		$cProTxt = $progress;
    }
	
	// Generate
	imagefilledrectangle($img, 0, 0, 436, 72, $trans);
	imagefillroundedrect($img,0,0,436,72,6,$cBg);
	if ($mode == 2) 
	{
		if(strlen($title)<28)
		{
			imagettftext($img, 16, 0, 85, 26, $cTitle, $bfont, $title);
		}
		else
		{
			imagettftext($img, 14, 0, 85, 26, $cTitle, $bfont, $title);
		}
		
		imagettftext($img, 10, 0, 86, 44, $cDescr, $font, $description);
		if (file_exists("./images/".$image) && is_file("./images/".$image)) {
			$logo = @imagecreatefrompng("./images/".$image);
			if(!$logo)
			$logo = imagecreatefrompng('images/invalid.png');
			//if ($status == 0)
			//	imagefilter($logo, IMG_FILTER_GRAYSCALE);
			ImageCopy($img, $logo, 4, 4, 0, 0, 64, 64);
		}
		else {
			$logo = imagecreatefrompng('images/1200.png');
			//if ($status == 0)
			//	imagefilter($logo, IMG_FILTER_GRAYSCALE);
			imagecopymerge($img, $logo, 4, 4, 0, 0, 64, 64, 100);
		}
	}
	else
	{
		if(strlen($d2['name'])<28)
		{
			imagettftext($img, 16, 0, 85, 26, $cTitle, $bfont, $d2['name']);
		}
		else
		{
			imagettftext($img, 14, 0, 85, 26, $cTitle, $bfont, $d2['name']);
		}
		imagettftext($img, 10, 0, 86, 44, $cDescr, $font, $d2['description']);
		if (file_exists("./images/".$d2['img']) && is_file("./images/".$d2['img'])) {
			$logo = @imagecreatefrompng("./images/".$d2['img']);
			if(!$logo)
			$logo = imagecreatefrompng('images/invalid.png');
			//if ($status == 0)
			//	imagefilter($logo, IMG_FILTER_GRAYSCALE);
			ImageCopy($img, $logo, 4, 4, 0, 0, 64, 64);
		}
		else {
			$logo = imagecreatefrompng('images/1200.png');
			//if ($status == 0)
			//	imagefilter($logo, IMG_FILTER_GRAYSCALE);
			imagecopymerge($img, $logo, 4, 4, 0, 0, 64, 64, 100);
		}
	}
	if ($damount > 1) {
		imagefilledrectangle($img, 86, 50, 347, 65, $cProgressbg);
		if($status == 1)
		{
			if ($fill != 0) // Prevent displaying of 1px bar
				imagefilledrectangle($img, 86, 50, (261+86), 65, $cProgress);
		}
		else
		{
			if ($fill != 0) // Prevent displaying of 1px bar
				imagefilledrectangle($img, 86, 50, (((261/$damount)*($fill > $damount ? $damount : $fill))+86), 65, $cProgress);
		}
		imagettftext($img, 8, 0, 350 + $xProText, 62, $cProTxt, $bfont, $tProTxt);
	}
	
	// Set the header
	header("Content-type: image/png");
	
	// WE'RE DONE!
	imagepng($img);
	imagedestroy($img);
	mysql_close();
?>